package com.epam.edu.command;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SignInCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String page;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            int userId = -1;
            for (int i = 0; i < cookies.length; i++) {
                if ("userId".equals(cookies[i].getName())) {
                    userId = Integer.parseInt(cookies[i].getValue());
                    session.setAttribute("userId", userId);
                    page = "/WEB-INF/jsp/admin.jsp";
                    return page;
                }
            }
        }
        return  "/WEB-INF/jsp/login.jsp";
    }
}
